clear all
set more off
capture log close
log using "$output/figure6.log", replace

version 16	

use "$data/ctctaxsim_mar1kid1.dta", clear
append using "$data/ctctaxsim_mar1kid2.dta"
append using "$data/ctctaxsim_mar1kid3.dta"
tab depx

sum fiitax earnings fedtaxlessctc v25 totctc
replace earnings=earnings*1000
replace ctc=ctc*1000
replace v25=v25*1000
replace totctc=totctc*1000
sum fiitax earnings fedtaxlessctc v25 totctc

gen avgtaxrate=fiitax/earnings
gen avgtaxrate2021=fedtaxlessctc/earnings	

gen avgtaxeitc=-v25/earnings
gen avgtaxctc=-totctc/earnings
gen avgtaxctc2021=0

gen avgtaxeitcctc=avgtaxeitc+avgtaxctc

gen avginctax=avgtaxrate- avgtaxeitcctc

tab year avginctax if earnings==40000 & depx==1
replace avginctax=avginctax*.04/.037875 if year==2017
tab year avginctax if earnings==40000 & depx==1

foreach var in avgtaxeitc avgtaxctc avgtaxctc2021 avginctax{
	replace `var'=-`var'
}

foreach var in avgtaxeitc avgtaxctc avgtaxctc2021 avginctax{
	replace `var'=`var'+1
}

bysort earnings depx: egen minavgtax=min(avgtaxrate)
gen difavgtax2=avgtaxrate2021-minavgtax
sum difavgtax2

bysort earnings depx: egen maxavgtax=max(avgtaxrate)
gen difavgtax=avgtaxrate2021-maxavgtax
sum difavgtax

gen difavgtax3=minavgtax-maxavgtax

sort earnings depx year 
order year earnings depx *avgtax* dif*
drop if year==2017
replace difavgtax3=-difavgtax3

*** create avg tax rates by $5000 bin x #kids to merge into income dist
gen inccat=.
forvalues i=5000(5000)100000{
	replace inccat=`i' if earnings>`i'-5000 & earnings<=`i'
}
	
bysort inccat depx year: egen avgtax = mean(difavgtax2)
replace difavgtax2=-difavgtax2

sort earnings depx
twoway line difavgtax2 earnings if year==2018 & depx==1, lp(-) pstyle(p1) || line difavgtax2 earnings if year==2018 & depx==2, lp(_) pstyle(p2) || line difavgtax2 earnings if year==2018 & depx==3, pstyle(p3) graphregion(color(white)) bgcolor(white) leg(order(1 "1 Kid" 2 "2 Kids" 3 "3 Kids") c(1) pos(5) ring(0)) ytitle("Change in Return to Work") xtitle("Earnings" "{bf:Panel A: Married Parent Family}") ylabel(0 "0%" -.05 "-5%" -.1 "-10%" -.15 "-15%" -.2 " ") 
graph save "$output/avgtaxinccatmar.gph", replace


use "$data/ctctaxsim_mar0kid1.dta", clear
append using "$data/ctctaxsim_mar0kid2.dta"
append using "$data/ctctaxsim_mar0kid3.dta"
tab depx

sum fiitax earnings fedtaxlessctc v25 totctc
replace earnings=earnings*1000
replace ctc=ctc*1000
replace v25=v25*1000
replace totctc=totctc*1000
sum fiitax earnings fedtaxlessctc v25 totctc

gen avgtaxrate=fiitax/earnings
gen avgtaxrate2021=fedtaxlessctc/earnings	

gen avgtaxeitc=-v25/earnings
gen avgtaxctc=-totctc/earnings
gen avgtaxctc2021=0

gen avgtaxeitcctc=avgtaxeitc+avgtaxctc

gen avginctax=avgtaxrate- avgtaxeitcctc

bysort depx: sum avgtax*

tab year avginctax if earnings==40000 & depx==1
replace avginctax=avginctax*.04/.037875 if year==2017
tab year avginctax if earnings==40000 & depx==1

bysort earnings depx: egen minavgtax=min(avgtaxrate)
gen difavgtax2=avgtaxrate2021-minavgtax
sum difavgtax2

bysort earnings depx: egen maxavgtax=max(avgtaxrate)
gen difavgtax=avgtaxrate2021-maxavgtax
sum difavgtax

gen difavgtax3=minavgtax-maxavgtax
drop if year==2017
foreach var in difavgtax2 difavgtax difavgtax3{
	replace `var'=-`var'
}
*** create avg tax rates by $5000 bin x #kids to merge into income dist
gen inccat=.
forvalues i=5000(5000)100000{
	replace inccat=`i' if earnings>`i'-5000 & earnings<=`i'
}
	
bysort inccat depx year: egen avgtax = mean(difavgtax2)

sort earnings depx
twoway line difavgtax2 earnings if year==2018 & depx==1, lp(-) pstyle(p1) || line difavgtax2 earnings if year==2018 & depx==2, lp(_) pstyle(p2) || line difavgtax2 earnings if year==2018 & depx==3, pstyle(p3) graphregion(color(white)) bgcolor(white) leg(order(1 "1 Kid" 2 "2 Kids" 3 "3 Kids") c(1) pos(5) ring(0)) ytitle("Change in Average Tax Rate") xtitle("Earnings" "{bf:Panel B: Unmarried Parent Family}") ylabel(0 "0%" -.05 "-5%" -.1 "-10%" -.15 "-15%" -.2 " ") 
graph save "$output/avgtaxinccatunmar.gph", replace

keep avgtax depx inccat
rename depx calckids
duplicates drop
unique calckids inccat
replace inccat=inccat/1000
gen married=0

graph combine "$output/avgtaxinccatmar.gph" "$output/avgtaxinccatunmar.gph" ,graphregion(color(white)) 
graph export "$output/figure6.png", replace

erase "$output/avgtaxinccatmar.gph" 
erase "$output/avgtaxinccatunmar.gph"

log close